home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / uux789 / uue.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  4KB  |  206 lines

  1. /*
  2.  *
  3.  * Uue -- encode a file so that it's printable ascii, short lines
  4.  *
  5.  * Slightly modified from a version posted to net.sources a while back,
  6.  * and suitable for compilation on the IBM PC
  7.  *
  8.  * modified for Lattice C on the ST - 11.05.85 by MSD
  9.  * modified for ALCYON on the ST -    10-24-86 by RDR
  10.  * modified a little more for MWC...  02/09/87 by JPHD
  11.  * (An optional first argument of the form: -nnumber (e.g. -500), will
  12.  * produce a serie of files that long, linked by the include statement,
  13.  * such files are automatically uudecoded by the companion program.)
  14.  * More mods, - ...           05/06/87 by jphd
  15.  * Mods for TOPS 20, and more.     08/06/87 by jphd
  16.  *     (remove freopen and rindex...change filename generation...)
  17.  * (A lot more to do about I/O speed, avoiding completely the stdio.h...)
  18.  *
  19.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24.  
  25. #define USAGE 
  26.  
  27. /* ENC is the basic 1 character encoding function to make a char printing */
  28. #define ENC(c) (((c) & 077) + ' ')
  29.  
  30. extern FILE  *fopen();
  31. FILE *fp, *outp;
  32. char ofname[80];
  33. int lenofname;
  34. int stdo = 0;
  35.  
  36. #ifdef ST
  37. #define READ "rb"
  38. #else
  39. #define READ "r"
  40. #endif
  41.  
  42. int part = 'a', chap = 'a';
  43. #define SEQMAX 'z'
  44. #define SEQMIN 'a'
  45. char seqc = SEQMAX;
  46.  
  47. int split = 0; fileln = 32000;
  48.  
  49. main(argc, argv)
  50. int argc; char *argv[];
  51. {
  52.     char *fname;
  53.  
  54.     if (argc < 2) {
  55.         fprintf(stderr, "Almost foolproof uuencode v3.1 06 Aug 1987\n");
  56.         fprintf(stderr, "Usage: uue [-n] inputfile [-]\n");
  57.         exit(2);
  58.         }
  59.     if (argv[1][0] == '-') {
  60.         fileln = -atoi(argv[1]);
  61.         if (fileln <= 0) {
  62.             fprintf(stderr, "Wrong file length arg.\n");
  63.             exit();
  64.         }
  65.         split = 1;
  66.         argv++;
  67.         argc--;
  68.     }
  69.     if ((fp=fopen(argv[1], READ))==NULL) {  /* binary input !!! */
  70.         fprintf(stderr,"Cannot open %s\n",argv[1]);
  71.         exit(1);
  72.     }
  73.     strcpy(ofname, argv[1]);
  74.     fname = ofname;
  75.     do {
  76.         if (*fname == '.')
  77.             *fname = '\0';
  78.     } while (*fname++);
  79.         /* 8 char prefix + .uue -> 12 chars MAX */
  80.     lenofname = strlen(ofname);
  81.     if (lenofname > 8) ofname[8] = '\0';
  82.     strcat(ofname,".uue");
  83.     lenofname = strlen(ofname);
  84.     if (!split && (argc > 2) && (argv[2][0] == '-')) {
  85.         stdo = 1;
  86.         outp = stdout;
  87.      } else {
  88.          makename();
  89.          if((outp = fopen(ofname, "w")) == NULL) {
  90.              fprintf(stderr,"Cannot open %s\n", ofname);
  91.              exit(1);
  92.              }
  93.     }
  94.     maketable();
  95.     fprintf(outp,"begin %o %s\n", 0644, argv[1]);
  96.     encode();
  97.     fprintf(outp,"end\n");
  98.     fclose(outp);
  99.     exit(0);
  100. }
  101.  
  102. /* create ASCII table so a mailer can screw it up and the decode
  103.  * program can restore the error.
  104.  */
  105. maketable()
  106. {
  107.     register int i, j;
  108.  
  109.     fputs("table\n", outp);
  110.     for(i = ' ', j = 0; i < '`' ; j++) {
  111.         if (j == 32)
  112.             putc('\n', outp);
  113.         fputc(i++, outp);
  114.     }
  115.     putc('\n', outp);
  116. }
  117.  
  118. /*
  119.  * Generate the names needed for single and multiple part encoding.
  120.  */
  121. makename()
  122. {
  123.     if (split) {
  124.         ofname[lenofname - 1] = part;
  125.         ofname[lenofname - 2] = chap;
  126.     }
  127. }
  128.  
  129. /*
  130.  * copy from in to out, encoding as you go along.
  131.  */
  132. encode()
  133. {
  134.     char buf[80];
  135.     register int i, n;
  136.     register int lines;
  137.     lines = 6;
  138.  
  139.     for (;;) {
  140.         n = fr(buf, 45);
  141.         putc(ENC(n), outp);
  142.         for (i = 0; i < n; i += 3)
  143.               outdec(&buf[i]);
  144.         putc(seqc, outp);
  145.         seqc--;
  146.         if (seqc < SEQMIN) seqc = SEQMAX;
  147.         putc('\n', outp);
  148.         ++lines;
  149.         if (split && (lines > fileln)) {
  150.             part++;
  151.             if (part > 'z') {
  152.                 part = 'a';
  153.                 if (chap == 'z')
  154.                     chap = 'a'; /* loop ... */
  155.                 else
  156.                     chap++;
  157.             }
  158.             makename();
  159.             fprintf(outp,"include %s\n",ofname);
  160.             fclose(outp);
  161.             if((outp = fopen(ofname, "w")) == NULL) {
  162.                 fprintf(stderr,"Cannot open %s\n",ofname);
  163.                 exit(1);
  164.             }
  165.             maketable();
  166.             fprintf(outp,"begin part %c %s\n",part,ofname);
  167.             lines = 6;
  168.         }
  169.         if (n <= 0)
  170.             break;
  171.     }
  172. }
  173.  
  174. /*
  175.  * output one group of 3 bytes, pointed at by p, on file f.
  176.  */
  177. outdec(p)
  178. register char *p;
  179. {
  180.     register int c1, c2, c3, c4;
  181.  
  182.     c1 = *p >> 2;
  183.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  184.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  185.     c4 = p[2] & 077;
  186.     putc(ENC(c1), outp);
  187.     putc(ENC(c2), outp);
  188.     putc(ENC(c3), outp);
  189.     putc(ENC(c4), outp);
  190. }
  191.  
  192. /* fr: like read but stdio */
  193. int fr(buf, cnt)
  194. register char *buf;
  195. register int cnt;
  196. {
  197.     register int c, i;
  198.     for (i = 0; i < cnt; i++) {
  199.         c = fgetc(fp);
  200.         if (feof(fp))
  201.             return(i);
  202.         buf[i] = c;
  203.     }
  204.     return (cnt);
  205. }
  206.